<template>
  <view class="container">
    <!-- 筛选栏 -->
    <view class="filter-bar">
      <view class="filter-section">
        <text class="filter-label">文书类型</text>
        <scroll-view scroll-x="true" class="type-scroll">
          <view 
            v-for="(type, index) in documentTypes" 
            :key="index" 
            class="type-item"
            :class="{ 'active': selectedType === type.value }"
            @click="filterByType(type.value)"
          >
            <text>{{ type.label }}</text>
          </view>
        </scroll-view>
      </view>
    </view>

    <!-- 文书列表 -->
    <view class="document-list">
      <view 
        v-for="(item, index) in filteredDocuments" 
        :key="item.id" 
        class="document-item"
        @click="downloadDocument(item)"
      >
        <view class="item-icon">
          <text class="icon-text">📄</text>
        </view>
        <view class="item-content">
          <text class="item-title">{{ item.title }}</text>
          <text class="item-description">{{ item.description }}</text>
          <view class="item-meta">
            <text class="item-type">{{ item.type }}</text>
            <text class="item-size">{{ item.size }}</text>
          </view>
        </view>
        <view class="item-action">
          <text class="download-btn">下载</text>
        </view>
      </view>
    </view>

    <!-- 无结果提示 -->
    <view v-if="filteredDocuments.length === 0" class="no-result">
      <text>暂无 {{ selectedType === 'all' ? '' : documentTypes.find(t => t.value === selectedType)?.label }} 类文书</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectedType: 'all', // 当前选中的类型
      documentTypes: [
        { label: '全部', value: 'all' },
        { label: '民事', value: 'civil' },
        { label: '刑事', value: 'criminal' },
        { label: '行政', value: 'administrative' },
        { label: '合同', value: 'contract' },
        { label: '公司', value: 'company' },
        { label: '婚姻', value: 'marriage' },
        { label: '劳动', value: 'labor' },
      ],
      // 模拟文书数据（实际项目应从后端获取）
      documents: [
        {
          id: 1,
          title: '民事起诉状',
          description: '适用于一般民事纠纷的起诉状模板',
          type: '民事',
          size: '2.1KB',
          downloadUrl: '/static/documents/civil_complaint.docx', // 假设路径
        },
        {
          id: 2,
          title: '民事答辩状',
          description: '被告针对民事起诉的答辩状模板',
          type: '民事',
          size: '1.8KB',
          downloadUrl: '/static/documents/civil_reply.docx',
        },
        {
          id: 3,
          title: '刑事起诉书',
          description: '人民检察院提起公诉的文书模板',
          type: '刑事',
          size: '2.5KB',
          downloadUrl: '/static/documents/criminal_indictment.docx',
        },
        {
          id: 4,
          title: '劳动合同',
          description: '标准劳动合同范本',
          type: '合同',
          size: '3.0KB',
          downloadUrl: '/static/documents/labor_contract.docx',
        },
        {
          id: 5,
          title: '离婚协议书',
          description: '双方协议离婚的标准模板',
          type: '婚姻',
          size: '2.2KB',
          downloadUrl: '/static/documents/divorce_agreement.docx',
        },
        {
          id: 6,
          title: '股权转让协议',
          description: '公司股东之间股权转让协议模板',
          type: '公司',
          size: '3.5KB',
          downloadUrl: '/static/documents/share_transfer.docx',
        },
        {
          id: 7,
          title: '行政复议申请书',
          description: '向上级行政机关申请复议的文书',
          type: '行政',
          size: '2.0KB',
          downloadUrl: '/static/documents/admin_review.docx',
        },
        {
          id: 8,
          title: '劳动仲裁申请书',
          description: '向劳动仲裁委员会提起仲裁的申请书',
          type: '劳动',
          size: '2.3KB',
          downloadUrl: '/static/documents/labor_arbitration.docx',
        },
      ]
    };
  },
  computed: {
    filteredDocuments() {
      if (this.selectedType === 'all') {
        return this.documents;
      }
      return this.documents.filter(doc => doc.type === this.selectedType);
    }
  },
  methods: {
    filterByType(type) {
      this.selectedType = type;
    },
    async downloadDocument(document) {
      // 模拟下载流程
      uni.showLoading({
        title: '下载中...'
      });

      try {
        // 1. 如果是静态资源，可以直接通过 URL 打开（在 App 环境下）
        // 2. 如果需要后端接口，调用 uni.downloadFile
        // 3. 这里模拟一个下载成功
        await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟网络延迟

        // 实际下载逻辑（以 uni.downloadFile 为例）
        // const downloadTask = uni.downloadFile({
        //   url: document.downloadUrl,
        //   success: (res) => {
        //     if (res.statusCode === 200) {
        //       // 成功下载，可提示用户打开或保存
        //       uni.saveFile({
        //         tempFilePath: res.tempFilePath,
        //         success: (saveRes) => {
        //           console.log('文件保存成功:', saveRes.savedFilePath);
        //           uni.showToast({
        //             title: '下载成功',
        //             icon: 'success'
        //           });
        //         },
        //         fail: (err) => {
        //           console.error('文件保存失败:', err);
        //           uni.showToast({
        //             title: '保存失败',
        //             icon: 'error'
        //           });
        //         }
        //       });
        //     }
        //   },
        //   fail: (err) => {
        //     console.error('下载失败:', err);
        //     uni.showToast({
        //       title: '下载失败',
        //       icon: 'error'
        //     });
        //   },
        //   complete: () => {
        //     uni.hideLoading();
        //   }
        // });

        // 模拟成功提示
        uni.hideLoading();
        uni.showToast({
          title: `已开始下载: ${document.title}`,
          icon: 'success',
          duration: 2000
        });

        console.log(`准备下载: ${document.downloadUrl}`);

      } catch (error) {
        console.error('下载失败:', error);
        uni.hideLoading();
        uni.showToast({
          title: '下载失败',
          icon: 'error'
        });
      }
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20rpx;
  background-color: #f8f8f8;
  min-height: 100vh;
}

.filter-bar {
  background-color: #fff;
  padding: 20rpx;
  border-radius: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  margin-bottom: 20rpx;
}

.filter-section {
  display: flex;
  flex-direction: column;
}

.filter-label {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
  font-weight: bold;
}

.type-scroll {
  white-space: nowrap;
}

.type-item {
  display: inline-block;
  padding: 10rpx 20rpx;
  margin-right: 20rpx;
  background-color: #f0f0f0;
  border-radius: 30rpx;
  font-size: 26rpx;
  color: #666;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.type-item.active {
  background-color: #007aff;
  color: white;
}

.document-list {
  background-color: #fff;
  border-radius: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  padding: 20rpx 0;
}

.document-item {
  display: flex;
  align-items: center;
  padding: 20rpx 30rpx;
  border-bottom: 1px solid #eee;
}

.document-item:last-child {
  border-bottom: none;
}

.item-icon {
  margin-right: 20rpx;
}

.icon-text {
  font-size: 40rpx;
}

.item-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.item-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.item-description {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.item-meta {
  display: flex;
  justify-content: space-between;
  font-size: 24rpx;
  color: #999;
}

.item-type {
  background-color: #e3f2fd;
  color: #007aff;
  padding: 4rpx 10rpx;
  border-radius: 10rpx;
}

.item-size {
  /* color: #999; */ /* 继承 item-meta 的颜色 */
}

.item-action {
  display: flex;
  align-items: center;
}

.download-btn {
  padding: 10rpx 20rpx;
  background-color: #007aff;
  color: white;
  border-radius: 30rpx;
  font-size: 26rpx;
}

.no-result {
  text-align: center;
  padding: 80rpx 0;
  color: #999;
  font-size: 28rpx;
}
</style>